import z from "zod";
import {
  ENUM_MessageCodeTemplate,
  buildMessageCode,
  valid_base_string,
  valid_base_number,
  valid_base_enum,
  valid_base_array,
  valid_id,
  valid_name,
  valid_price,
  valid_phone,
  valid_ratio,
  valid_desc,
  valid_base_boolean,
} from "./core/index";

const name = valid_name("套餐名称");
const price = valid_price("原价");
const actualPrice = valid_price("实价");
const points = valid_base_number("积分").default(0);
const term = valid_base_number("有效期").default(0);
const isUse = valid_base_boolean("是否开启");

/** 套餐基本参数对象 */
export const packageBaseSchema = z.object({
  name,
  price,
  actualPrice,
  points,
});

export interface IPackageBase extends z.infer<typeof packageBaseSchema> {}

/** 储值 */
const baseAmount = valid_price("基础金额").default(0);
const giveAmount = valid_price("赠送金额").default(0);
const balanceAmount = valid_price("实际金额").default(0);
const discount = valid_ratio("折扣").default(0);

/** 储值参数对象 */
export const storeSchema = z.object({
  isUse,
  baseAmount,
  giveAmount,
  balanceAmount,
  discount,
  term,
});

export interface IPackageStore extends z.infer<typeof storeSchema> {}

/** 计次 */
const productName = valid_base_string("产品名称");
const baseCount = valid_base_number("基础次数")
  .min(1, buildMessageCode(ENUM_MessageCodeTemplate.min, "基础次数"))
  .default(1);
const giveCount = valid_base_number("赠送次数")
  .min(0, buildMessageCode(ENUM_MessageCodeTemplate.min, "基础次数"))
  .default(0);
const balanceCount = valid_base_number("剩余次数")
  .min(1, {
    message: buildMessageCode(ENUM_MessageCodeTemplate.min, "剩余次数"),
  })
  .default(1);
const products = valid_base_array(
  z.object({
    name: productName,
    price,
    actualPrice,
    baseCount,
    giveCount,
    balanceCount,
    term,
  }),
  "计次商品"
).min(1, {
  message: buildMessageCode(ENUM_MessageCodeTemplate.empaty, "计次商品"),
});
// ).refine(
//   (value) => {
//     return value.length > 0;
//   },
//   {
//     message: buildMessageCode(ENUM_MessageCodeTemplate.empaty, "产品"),
//     path: ["products"], // path of error
//   }
// );
export interface IProduct {
  name: string;
  price: number;
  actualPrice: number;
  baseCount: number;
  giveCount: number;
  balanceCount: number;
  term: number;
}

export const counterSchema = z.object({
  isUse,
  products,
});

export interface IPackageCounter extends z.infer<typeof counterSchema> {}

/** 计时 */
export const timeSchema = z.object({
  isUse,
  term,
});

export interface IPackageTime extends z.infer<typeof timeSchema> {}

/** 赠品-优惠券 */
const coupons = valid_base_array(
  z.object({
    name,
    price,
    actualPrice,
    points,
    term,
  }),
  "优惠券"
);

export const couponSchema = z.object({
  coupons,
});

export interface IPackageCoupon extends z.infer<typeof couponSchema> {}

export const packageSchema = z.union([
  packageBaseSchema,
  storeSchema,
  counterSchema,
  timeSchema,
  couponSchema,
]);

export interface IReqCreatePackage
  extends IPackageBase,
    IPackageStore,
    IPackageCounter,
    IPackageTime,
    IPackageCoupon {}
export interface IReqUpdatePackage extends IReqCreatePackage {
  id: string;
}
export interface IRspPackage extends IReqUpdatePackage {
  createdAt: Date;
}
